/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.start.controller;

import com.itgrail.pilot.app.command.cmo.AddDingCmd;
import com.itgrail.pilot.app.command.cmo.DingRobotNotifyCmd;
import com.itgrail.pilot.domain.user.ports.DingRobotPort;
import com.itgrail.pilot.infrastructure.tunnel.util.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itgrail.grail.cqrs.command.CommandBus;
import com.itgrail.grail.dto.ResultData;

/**
 * @author xujin
 */
@RestController
@RequestMapping("/admin/notify")
@Api("通知相关操作")
public class NotifyController {

    @Autowired
    private CommandBus commandBus;

    @Autowired
    private DingRobotPort dingRobotPort;

    @PostMapping("/notifyDing")
    @ApiOperation(value = "通知钉钉消息")
    public ResultData getDeptTree(@RequestBody AddDingCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @GetMapping("/notifyRobot")
    @ApiOperation(value = "通知钉钉消息")
    public ResultData notifyRobot() {
        dingRobotPort.robotNotify();
        return ResultData.success("请求成功");
    }

    @PostMapping("/notifyRobotTime")
    @ApiOperation(value = "通知钉钉机器人消息")
    public ResultData notifyRobotTime(@RequestBody DingRobotNotifyCmd dingRobotNotifyCmd) {
        dingRobotPort.robotTimeNotify(dingRobotNotifyCmd.getTime(), dingRobotNotifyCmd.getDeptId(), dingRobotNotifyCmd.getContent());
        return ResultData.success("请求成功");
    }
}
